// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	// 图包类型管理
	// 获取图包类型 ny-imgpack-tag 的所有数据
	async getImagepackTag() {
		// 获取数据库对象，拿到表
		// 获取数据库对象，拿到表
		const db = uniCloud.database();
		const indexAll = db.collection("ny-imgpack-tag");
		let res = await indexAll.where({}).get()
		return {
			code: 200,
			msg: '查询成功',
			data: res
		}
	},
	async addimgpackTag(item){
		const db = uniCloud.database();
		const indexAll = db.collection("ny-imgpack-tag");
		let res = await indexAll.add(item)
		return {
			code: 200,
			msg: '添加成功！！',
			data: res
		}
	},
	
	// 标签图标数据表 ny-tag-icon
	async gettagIcon() {
		// 获取数据库对象，拿到表
		// 获取数据库对象，拿到表
		const db = uniCloud.database();
		const indexAll = db.collection("ny-tag-icon");
		let res = await indexAll.get()
		return {
			code: 200,
			msg: '查询成功',
			data: res.data
		}
	},
	
	// 原创作者管理 ny-authorship-user
	// 获取原创作者
	async getauthorshipUser(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-authorship-user");
		let res
		if (item != '') {
			res = await collection.where({
				'_id': item
			}).get()
		} else {
			res = await collection.get()
		}
		return {
			code: 200,
			msg: '查询成功',
			data: res
		}
	},


	// 商品相关函数  ny-goods-picture
	// 添加商品
	async addgoodsPicture(event) {
		const db = uniCloud.database();
		const collection = db.collection("ny-goods-picture");
		const res = await collection.add(event) //event为客户端上传的参数
		return {
			code: 200,
			msg: '添加成功!!',
			data: res
		}
	},
	// 获取商品数据
	async getgoodsPicture(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-goods-picture");
		let res
		if (item != '') {
			res = await collection.where({
				'author_id': item
			}).get()
		} else {
			res = await collection.get()
		}
		return {
			code: 200,
			msg: '查询成功',
			data: res
		}
	},
	// 修改商品数据
	async updategoodsPicture(_id,item){
		const db = uniCloud.database();
		const collection = db.collection("ny-goods-picture");
		let res = await collection.doc(_id).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	},
	// 商品销量
	async goodsPictureVolume(itam){
		let _id = '6625ecad3d029c65e993d2e8'
		const db = uniCloud.database();
		const collection = db.collection("ny-goods-picture");
		let res = await collection.where({'_id':_id}).get()
		let volume = res.data.volume-0+1
		// let rea = await collection.doc(_id).update({"volume":volume})
		return {
			code: 200,
			msg: '修改成功!!',
			data: volume
		}
	},

	// 用户信息相关 ny-user 
	//微信登录openid 验证openid是否存在
	async login(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		let appid = 'wx912903619c27f196'
		let secret = 'd21ee07337d9a0b75fd64a270a76657a'
		let url =
			`https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${item}&grant_type=authorization_code`

		let res = await uniCloud.httpclient.request(url, {
			dataType: "json"
		})
		let openid = res.data.openid
		let login = false
		let rea = await collection.where({
			'openid': openid
		}).get()
		if (rea.data.length != 0) {
			login = true
		}
		return {
			openid,
			login,
			rea
		}

	},
	// 添加用户
	async addwxUser(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		const res = await collection.add(item)
		return {
			code: 200,
			msg: '添加成功!!',
			data: res
		}
	},
	// 修改用户
	async updatewxUser(itam, item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		let res = await collection.doc(itam).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	},



	// 图包订单 ny-picture-indent
	// 添加订单
	async addpictureIndent(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-picture-indent");
		const res = await collection.add(item)
		return {
			code: 200,
			msg: '添加成功!!',
			data: res
		}
	},
	// 查找订单
	async loopictureIndent(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-picture-indent");
		let res = await collection.where({
			"userid": item
		}).get()
		return {
			code: 200,
			msg: '查找成功!!',
			data: res.data
		}
	},
	async loopictureIndentall(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-picture-indent");
		let res = await collection.where(item).get()
		return {
			code: 200,
			msg: '查找成功!!',
			data: res.data
		}
	},
	// 修改订单
	async updatepictureIndent(_id, item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-picture-indent");
		let res = await collection.doc(_id).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	},


	// 轮播图相关API ny-swiper
	async getnySwiper() {
		const db = uniCloud.database();
		const collection = db.collection("ny-swiper");
		let res = await collection.get()
		return {
			code: 200,
			msg: '查找成功!!',
			data: res.data
		}
	},
	// 修改轮播图片
	async updatenySwiper(_id, item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-swiper");
		let res = await collection.doc(_id).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	}
	

}